package com.jfinal.club._admin.wordcolor;

import com.jfinal.club.common.model.Wordcolor;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import java.util.Date;

/**
 * wordcolor 管理业务
 */
public class WordcolorAdminService {

	private Wordcolor dao = new Wordcolor().dao();

	/**
	 * 色彩义分页
	 */
	public Page<Wordcolor> paginate(int pageNum) {
		return dao.paginate(pageNum, 10, "select *", "from wordcolor order by id desc");
	}

	/**
	 * 判断色彩义名称是否存在
	 * @param wordcolorId 当前 wordcolor 对象的 id 号，如果 wordcolor 对象还未创建，提供一个小于 0 的值即可
	 * @param name 色彩义名
	 */
	public boolean exists(int wordcolorId, String name) {
		name = name.toLowerCase().trim();
		String sql = "select id from wordcolor where lower(name) = ? and id != ? limit 1";
		Integer id = Db.queryInt(sql, name, wordcolorId);
		return id != null;
	}

	/**
	 * 创建色彩义
	 */
	//Ret save(int accountId, Wordcolor wordcolor,Word word)
	public Ret save(int accountId,Wordcolor wordcolor) {
		//word.setContent(word.getContent());
		wordcolor.setContent(wordcolor.getContent().trim());
		wordcolor.setCreateAt(new Date());
		wordcolor.save();
		return Ret.ok("msg", "创建成功");
	}

	public Wordcolor edit(int id) {
		return dao.findById(id);
	}

	public Ret update(Wordcolor wordcolor) {
		wordcolor.setContent(wordcolor.getContent());
		wordcolor.update();
		return Ret.ok("msg", "修改成功");
	}
	/**
	 * 删除 wordcolor
	 */
	public Ret delete(int wordcolorId) {
		/*Integer accountId = Db.queryInt("select accountId from wordcolor where id=? limit 1", wordcolorId);
		if (accountId != null) {
			MyWordcolorService.me.delete(accountId, wordcolorId);
			return Ret.ok("msg", "wordcolor 删除成功");
		} else {
			return Ret.fail("msg", "wordcolor 删除失败");
		}*/
		dao.deleteById(wordcolorId);
		return Ret.ok("msg", "wordcolor 删除成功");
	}
}
